using System;
using System.Collections.Generic;
using System.Text;

using Wrath.Configuration;
using System.IO;
using System.Drawing;

using ICSharpCode.SharpZipLib.Zip;
using Wrath.Xml;

namespace Wrath.RuleForge
{
    public static class Globals
    {
        public static ConfigurationWorker Config;

        static Globals()
        {
            Config = new ConfigurationWorker();
        }

        public static class RuleSets
        {
            private static List<string> rulesets;
            public static int Count { get { return rulesets.Count; } }

            static RuleSets()
            {
                rulesets = new List<string>();
            }

            public static bool Contains(string ruleSetName)
            {
                return rulesets.Contains(ruleSetName);
            }

            public static void AddRuleSet(string ruleSetName)
            {
                if (!Contains(ruleSetName))
                {
                    rulesets.Add(ruleSetName);
                }
            }

            public static void RemoveRuleSet(string ruleSetName)
            {
                if (Contains(ruleSetName))
                {
                    rulesets.Remove(ruleSetName);
                }
            }

            public static string GetRuleSet(int index)
            {
                return rulesets[index];
            }
        }

        public static class FantasyGroundsPaths
        {
            public static string Data;
            public static string Modules;
            public static string RuleSets;
            public static string CurrentRuleSet;
        }

        public static class RuleForgePaths
        {
            public static string Data;
            public static string Temp;
            public static string Templates;
            public static string UserTemplates;
            public static string Cache;
            public static string ModuleCache;

            static RuleForgePaths()
            {
                string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                Data = appData + "\\RuleForge";
                Cache = Globals.RuleForgePaths.Data + "\\Cache";
                ModuleCache = Globals.RuleForgePaths.Cache + "\\Modules";
                Temp = Globals.RuleForgePaths.Data + "\\Temp";
                Templates = Globals.RuleForgePaths.Data + "\\Templates";
                UserTemplates = Globals.RuleForgePaths.Templates + "\\User";

                Directory.CreateDirectory(Globals.RuleForgePaths.Data);
                Directory.CreateDirectory(Globals.RuleForgePaths.Cache);
                Directory.CreateDirectory(Globals.RuleForgePaths.ModuleCache);
                Directory.CreateDirectory(Globals.RuleForgePaths.Temp);
                Directory.CreateDirectory(Globals.RuleForgePaths.Templates);
                Directory.CreateDirectory(Globals.RuleForgePaths.UserTemplates);
            }
        } // RuleForgePaths

        public sealed class ModuleInformation : IDisposable
        {
            private string filename;
            private string name;
            private string author;
            private string ruleset;
            private string categoryname;
            private Image thumbnail;

            public string Filename { get { return filename; } set { filename = value; } }
            public string Name { get { return name; } set { name = value; } }
            public string Author { get { return author; } set { author = value; } }
            public string RuleSet { get { return ruleset; } set { ruleset = value; } }
            public string Category { get { return categoryname; } set { categoryname = value; } }
            public Image Thumbnail { get { return thumbnail; } set { thumbnail = value; } }

            public ModuleInformation()
            {
                filename = "";
                name = "";
                author = "";
                ruleset = "";
                categoryname = "";
                thumbnail = new Bitmap(76, 86);
            }

            public ModuleInformation(string filename, string name, string author, string ruleset, string category, Image thumbnail)
            {
                this.filename = filename;
                this.name = name;
                this.author = author;
                this.ruleset = ruleset;
                this.categoryname = category;
                this.thumbnail = thumbnail;
            }
        
            #region IDisposable Members

            public void  Dispose()
            {
 	            thumbnail.Dispose();
            }

            #endregion
        }

        public static class Modules
        {
            private static List<ModuleInformation> modules;

            public static int Count { get { return modules.Count; } }

            static Modules()
            {
                modules = new List<ModuleInformation>();
            }

            public static void Dispose()
            {
                foreach (ModuleInformation mi in modules)
                {
                    mi.Dispose();
                }

                modules.Clear();
            }

            public static ModuleInformation GetModule(int index)
            {
                return modules[index];
            }

            public static ModuleInformation GetModuleByName(string moduleName)
            {
                return modules.Find(new Predicate<ModuleInformation>(
                    delegate(ModuleInformation mi)
                    {
                        if (moduleName == mi.Name)
                            return true;

                        return false;
                    }
                    ));
            }

            public static ModuleInformation GetModuleByFileName(string filename)
            {
                return modules.Find(new Predicate<ModuleInformation>(
                    delegate(ModuleInformation mi)
                    {
                        if (filename == mi.Filename)
                            return true;

                        return false;
                    }
                    ));
            }


            public static void RemoveModuleByName(string moduleName)
            {
                ModuleInformation mi = GetModuleByName(moduleName);
                modules.Remove(mi);
            }

            public static void RemoveModuleByFileName(string moduleFilename)
            {
                ModuleInformation mi = GetModuleByFileName(moduleFilename);
                modules.Remove(mi);
            }

            public static void AddModule(ModuleInformation moduleInformation)
            {
                modules.Add(moduleInformation);
            }

            public static void AddModule(string filename, string name, string author, string ruleset, string category, Image thumbnail)
            {
                ModuleInformation mi = new ModuleInformation(filename, name, author, ruleset, category, thumbnail);
                modules.Add(mi);
            }

            public static void PackageModule(string moduleName)
            {
                string basePath = RuleForgePaths.ModuleCache + "\\" + moduleName + "\\";
                if (Directory.Exists(basePath))
                {
                    //string[] moduleFiles = Directory.GetFiles(basePath);

                    FastZip zip = new FastZip();
                    zip.CreateZip(FantasyGroundsPaths.Modules + "\\" + moduleName + ".mod", basePath, true, "");

                    //Zip.ZipHelper.CreateZipFile(FantasyGroundsPaths.Modules + "\\" + moduleName + ".mod", basePath, moduleFiles);
                }
            }

            public static void PackageModule(string moduleName, string basePath)
            {
                if (Directory.Exists(basePath))
                {
                    //string[] moduleFiles = Directory.GetFiles(basePath);

                    FastZip zip = new FastZip();
                    zip.CreateZip(FantasyGroundsPaths.Modules + "\\" + moduleName + ".mod", basePath, true, "");                    
                    //Zip.ZipHelper.CreateZipFile(FantasyGroundsPaths.Modules + "\\" + moduleName + ".mod", basePath, moduleFiles);
                }
            }
        }      
        
    }
}
